package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyThemeQuery;
import cn.wolfcode.luowowo.repository.StrategyCommentRepository;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 攻略评论实现
 */
@Service
//@Transactional mongodb事务必须复制集方式
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    //spring-data-mongodb中的查询方式有两种repository(不能分页操作)和template
    //Spring-boot集成redis持久化数据库
    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {

        strategyComment.setId(null);
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {

        repository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {

        repository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> listAll() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        Query query = new Query();

        if(StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));

        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,
                qo.getPageSize(), Sort.Direction.DESC,"createTime");

        return DBHelper.query(template,StrategyComment.class,query,pageable);
    }

    // 攻略评论点赞
    @Override
    public void commentThumb(String cid, String uid) {
        // 查询评论对象,获取点赞用户集合
        StrategyComment comment = this.get(cid);
        List<String> uidList = comment.getThumbuplist();
        // 判断当前永辉id是否存在集合中
        if(!uidList.contains(uid)){
            // 如果不存在,表示点赞操作 点赞+1 用户id添加到集合中
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            uidList.add(uid);
        }else{
            // 如果存在表示取消点点赞 点赞数 -1 用户id移出集合
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            uidList.remove(uid);
        }
        // 更新
        this.update(comment);
    }
}
